// src/router.js
const express = require('express')
//给req增加一个cookies属性
const cookieParser = require('cookie-parser')
//给req增加一个body属性
const bodyParser = require('body-parser')
//文件写入和读取用
const fs = require('fs')
const router = express.Router()
router.use(bodyParser.urlencoded({extended:false}))
router.use(cookieParser())

//stu路由
router.get('/stu',(req,res)=>{
  let id = req.query.id;
  res.sendFile(__dirname+'/homework/'+id+'.json')
});

//a/b路由
router.get('/a/b',(req,res)=>{
  res.send(req.query);
});
router.post('/a/b', (req, res) => {
  res.send(req.body);
});

//user路由
router.get('/user', (req, res) => {
  setTimeout(() => {
    res.send('<h1>hello</h1>');
  }, 3000);
  
});

//login路由
router.post('/login',(req,res)=>{
  res.send(req.cookies);
})

//cookie路由
router.get('/cookie',(req,res)=>{
  res.send('cookie')
})

//cookie1路由
router.get('/cookie1',()=>{
  res.send('cookie1');
})
//cookie2路由
router.get('/cookie2', () => {
  res.send('cookie2');
})
//接口，用来把router传递出去
module.exports=router